package com.first.nell.components.bar

import android.annotation.SuppressLint
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import com.first.nell.R
import com.first.nell.components.dialog.CustomerServiceDialog
import com.first.nell.databinding.ViewFirstNellTopNavigationBarBinding
import com.first.nell.extend.getFirstNellParentFirstNellActivity
import com.first.nell.extend.startFirstNellToFirstNellNextFirstNellActivity
import com.first.nell.extend.startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop
import com.first.nell.home.HomeActivity
import com.first.nell.launch.LaunchActivity

/**
<declare-styleable name="FirstNellTopNavigationBar">
<attr name="cwtnbCenterTextColor" format="color" />
<attr name="cwtnbCenterText" format="string" />
<attr name="cwtnbLeftImageRes" format="reference" />
<attr name="cwtnbRightImageRes" format="reference" />
<attr name="cwtnbLeftImageHidden" format="boolean" />
<attr name="cwtnbLeftTitleHidden" format="boolean" />
<attr name="cwtnbLeftTitleColor" format="color" />
<attr name="cwtnbRightImageHidden" format="boolean" />
<attr name="cwtnbLeftActionMode" format="integer" />
<attr name="cwtnbRightActionMode" format="integer" />
</declare-styleable>
 * */
class TopNavigationBar : RelativeLayout {

    lateinit var mFirstNellViewFirstNellBinding: ViewFirstNellTopNavigationBarBinding
    private lateinit var vFirstNellRootFirstNellView: View

    private var cwtnbCenterTextColor: Int = 0
    private var cwtnbCenterText: String? = null
    private var cwtnbLeftTitle: String? = null
    private var cwtnbLeftTitleColor: Int = 0
    private var cwtnbLeftImageRes: Int = 0
    private var cwtnbRightImageRes: Int = 0
    private var cwtnbRightImageHidden: Boolean = true
    private var cwtnbLeftImageHidden: Boolean = false
    private var cwtnbLeftTitleHidden: Boolean = false
    private var cwtnbLeftActionMode: Int = 0
    private var cwtnbRightActionMode: Int = 0
    private var cwtnbBottomLineHidden: Boolean = false

    private var mFirstNellSupportFirstNellFragmentFirstNellManager: FragmentManager? = null

    private var mFirstNellOnFirstNellActionFirstNellClickFirstNellListener: (Int) -> Unit = {}

    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : this(
        context,
        attrs,
        defStyleAttr,
        0
    )

    @SuppressLint("Recycle")
    constructor(
        context: Context?,
        attrs: AttributeSet?,
        defStyleAttr: Int,
        defStyleRes: Int,
    ) : super(
        context,
        attrs,
        defStyleAttr,
        defStyleRes
    ) {
        attrs?.let {
            context?.let { cxt ->
                val array = cxt.obtainStyledAttributes(attrs, R.styleable.TopNavigationBar
                )
                cwtnbCenterTextColor = array.getColor(
                    R.styleable.TopNavigationBar_cwtnbCenterTextColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )
                cwtnbCenterText =
                    array.getString(R.styleable.TopNavigationBar_cwtnbCenterText)

                cwtnbLeftTitle =
                    array.getString(R.styleable.TopNavigationBar_cwtnbLeftTitle)
                cwtnbLeftImageRes =
                    array.getResourceId(R.styleable.TopNavigationBar_cwtnbLeftImageRes, 0)
                cwtnbLeftImageHidden = array.getBoolean(
                    R.styleable.TopNavigationBar_cwtnbLeftImageHidden,
                    false
                )
                cwtnbLeftTitleHidden = array.getBoolean(
                    R.styleable.TopNavigationBar_cwtnbLeftTitleHidden,
                    false
                )
                cwtnbLeftTitleColor = array.getColor(
                    R.styleable.TopNavigationBar_cwtnbLeftTitleColor,
                    ContextCompat.getColor(cxt, R.color.main_black)
                )

                cwtnbRightImageRes =
                    array.getResourceId(R.styleable.TopNavigationBar_cwtnbRightImageRes, 0)
                cwtnbRightImageHidden = array.getBoolean(
                    R.styleable.TopNavigationBar_cwtnbRightImageHidden,
                    false
                )

                cwtnbLeftActionMode = array.getInteger(
                    R.styleable.TopNavigationBar_cwtnbLeftActionMode,
                    resources.getInteger(R.integer.ActionModeFinish)
                )
                cwtnbRightActionMode = array.getInteger(
                    R.styleable.TopNavigationBar_cwtnbRightActionMode,
                    resources.getInteger(R.integer.ActionModeCustomerService)
                )

                cwtnbBottomLineHidden = array.getBoolean(
                    R.styleable.TopNavigationBar_cwtnbBottomLineHidden,
                    false
                )

                array.recycle()
            }
        }
        context?.let {
            vFirstNellRootFirstNellView = View.inflate(it, R.layout.view_first_nell_top_navigation_bar, this)
            mFirstNellViewFirstNellBinding = ViewFirstNellTopNavigationBarBinding.bind(vFirstNellRootFirstNellView)
            initFirstNellSubviews()
        }
    }

    private fun initFirstNellSubviews() {
        try {
            mFirstNellViewFirstNellBinding.textTopNavigationBarCenter.text = cwtnbCenterText
            mFirstNellViewFirstNellBinding.textTopNavigationBarCenter.setTextColor(cwtnbCenterTextColor)

            mFirstNellViewFirstNellBinding.imageTopNavigationBarLeft.setImageResource(cwtnbLeftImageRes)
            mFirstNellViewFirstNellBinding.imageTopNavigationBarRight.setImageResource(cwtnbRightImageRes)

            mFirstNellViewFirstNellBinding.textTopNavigationBarLeft.text = (cwtnbLeftTitle)
            mFirstNellViewFirstNellBinding.textTopNavigationBarLeft.setTextColor(cwtnbLeftTitleColor)

            if (cwtnbLeftImageHidden) {
                mFirstNellViewFirstNellBinding.imageTopNavigationBarLeft.visibility = View.GONE
            } else {
                mFirstNellViewFirstNellBinding.imageTopNavigationBarLeft.visibility = View.VISIBLE
            }

            if (cwtnbLeftTitleHidden) {
                mFirstNellViewFirstNellBinding.textTopNavigationBarLeft.visibility = View.GONE
            } else {
                mFirstNellViewFirstNellBinding.textTopNavigationBarLeft.visibility = View.VISIBLE
            }

            if (cwtnbRightImageHidden) {
                mFirstNellViewFirstNellBinding.imageTopNavigationBarRight.visibility = View.GONE
            } else {
                mFirstNellViewFirstNellBinding.imageTopNavigationBarRight.visibility = View.VISIBLE
            }

            if (cwtnbBottomLineHidden) {
                mFirstNellViewFirstNellBinding.vTopNavigationBarLine.visibility = View.GONE
            } else {
                mFirstNellViewFirstNellBinding.vTopNavigationBarLine.visibility = View.VISIBLE
            }

            mFirstNellViewFirstNellBinding.textTopNavigationBarLeft.setOnClickListener {
                setFirstNellBarFirstNellClickFirstNellAction(isLeftAction = true)
            }

            mFirstNellViewFirstNellBinding.imageTopNavigationBarLeft.setOnClickListener {
                setFirstNellBarFirstNellClickFirstNellAction(isLeftAction = true)
            }

            mFirstNellViewFirstNellBinding.imageTopNavigationBarRight.setOnClickListener {
                setFirstNellBarFirstNellClickFirstNellAction(isLeftAction = false)
            }
        } catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
    }

    private fun setFirstNellBarFirstNellClickFirstNellAction(isLeftAction: Boolean) {
        if (isLeftAction) {
            when (cwtnbLeftActionMode) {
                resources.getInteger(R.integer.ActionModeClick) -> {
                    mFirstNellOnFirstNellActionFirstNellClickFirstNellListener.invoke(R.integer.NavbarActionLeft)
                }
                resources.getInteger(R.integer.ActionModeFinish) -> {
                    val gActivity = getFirstNellParentFirstNellActivity()
                    gActivity?.finish()
                }
                resources.getInteger(R.integer.ActionModeToRoot) -> {
                    val gActivity = getFirstNellParentFirstNellActivity()
                    gActivity?.let { it1 ->
                        if (it1 is AppCompatActivity) {
                            it1.startFirstNellToFirstNellNextFirstNellActivityFirstNellForFirstNellSingleFirstNellTop<HomeActivity>(HashMap<String, String>())
                        }
                    }
                }
                resources.getInteger(R.integer.ActionModeCustomerService) -> {
                    val gActivity = getFirstNellParentFirstNellActivity()
                    gActivity?.let {
                        CustomerServiceDialog.showFirstNellDialog(it.supportFragmentManager)
                    }
                }
            }
        } else {
            when (cwtnbRightActionMode) {
                resources.getInteger(R.integer.ActionModeClick) -> {
                    mFirstNellOnFirstNellActionFirstNellClickFirstNellListener.invoke(R.integer.NavbarActionRight)
                }
                resources.getInteger(R.integer.ActionModeFinish) -> {
                    val gActivity = getFirstNellParentFirstNellActivity()
                    gActivity?.finish()
                }
                resources.getInteger(R.integer.ActionModeToRoot) -> {
                    val gActivity = getFirstNellParentFirstNellActivity()
                    gActivity?.let { it1 ->
                        if (it1 is AppCompatActivity) {
                            it1.startFirstNellToFirstNellNextFirstNellActivity<LaunchActivity>(HashMap<String, String>())
                        }
                    }
                }
                resources.getInteger(R.integer.ActionModeCustomerService) -> {
                    val gActivity = getFirstNellParentFirstNellActivity()
                    gActivity?.let {
                        CustomerServiceDialog.showFirstNellDialog(it.supportFragmentManager)
                    }
                }
            }
        }
    }

    fun setFirstNellLeftFirstNellText(leftText:String){
        cwtnbLeftTitle = leftText
        mFirstNellViewFirstNellBinding.textTopNavigationBarLeft.text = (cwtnbLeftTitle)
    }

    fun setFirstNellCenterFirstNellText(leftText:String){
        cwtnbCenterText = leftText
        mFirstNellViewFirstNellBinding.textTopNavigationBarCenter.text = (cwtnbCenterText)
    }

    fun setFirstNellFragmentFirstNellManager(manager: FragmentManager) {
        mFirstNellSupportFirstNellFragmentFirstNellManager = manager
    }

    fun setFirstNellOnFirstNellActionFirstNellClickFirstNellListener(listener: (Int) -> Unit) {
        mFirstNellOnFirstNellActionFirstNellClickFirstNellListener = listener
    }
}